<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">

      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同标题" prop="contractTitle">
        <el-input
          v-model="queryParams.contractTitle"
          placeholder="请输入合同标题"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同类型" prop="contractType">
        <el-select v-model="queryParams.contractType" clearable placeholder="请选择" size="small">
          <el-option label="设备" value="1"></el-option>
          <el-option label="水电" value="2"></el-option>
          <el-option label="土建" value="3"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          v-hasPermi="['contractManagement:technical:add']"
          @click="handleAddFn"
        >新增</el-button>
        <el-button type="warning" size="mini" icon="el-icon-refresh" v-hasPermi="['contractManagement:technical:sysnc']" :disabled="loading" @click="sysncAll">批量同步</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list"  border size="mini" :height="listHeight || fillHeight('listHeight',['queryForm','add','pagination'],['hamburger-container','tags-view-container'], 130)">
      <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
      <el-table-column label="项目名称" align="center"  prop="projectName" :show-overflow-tooltip="true" />
      <el-table-column label="合同标题" align="center"  prop="contractTitle" :show-overflow-tooltip="true" />
      <el-table-column label="合同编码" prop="contractNumber" align="center"/>
      <el-table-column label="合同金额" prop="contractMoney" align="center"/>
      <!-- <el-table-column label="创建人" prop="createBy" align="center"/> -->

      <el-table-column label="合同类型" prop="contractType" align="center" >
        <template slot-scope="scope">
          <span v-if="scope.row.contractType == '1'">设备</span>
          <span v-if="scope.row.contractType == '2'">水电</span>
          <span v-if="scope.row.contractType == '3'">土建</span>
        </template>
      </el-table-column>

      <el-table-column label="创建人" prop="createBy" align="center" >
        <template slot-scope="scope">
          {{scope.row.createName}} {{scope.row.createBy}}
        </template>
      </el-table-column>

      <el-table-column label="创建时间" prop="createTime" align="center"/>

      <el-table-column label="匹配结束时间" prop="matchInfoEndTime" :show-overflow-tooltip="true" align="center"/>
      <el-table-column label="合同结束时间" prop="contractInfoEndTime" :show-overflow-tooltip="true" align="center"/>

      <el-table-column label="状态" prop="status" align="center" >
        <template slot-scope="scope">
          <span v-show="scope.row.status == '' || scope.row.status == 'save' || scope.row.status == null ">保存</span>
          <span v-show="scope.row.status == 'active'">审批中</span>
          <span v-show="scope.row.status == 'completed'">审批完成</span>
          <span v-show="scope.row.status == 'cancel'">作废、取消、删除</span>
          <span v-show="scope.row.status == 'reject'">退回、驳回</span>
        </template>
      </el-table-column>
      <el-table-column label="是否同步" prop="isLegalSync" align="center" >
        <template slot-scope="scope">
          <span v-if="scope.row.isLegalSync==1">已同步</span>
          <span v-else-if="scope.row.status=='completed'">未同步</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="200" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-order"
            @click="handleDetails(scope.row.id,scope.row.status)"
          >详情</el-button>
          <el-dropdown trigger="click">
            <el-button
              size="mini"
              type="text"
              style="margin-left: 10px"
            >更多<i class="el-icon-arrow-down el-icon--right"></i></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                  size="mini"
                  type="text"
                  v-show=" scope.row.status != 'active' && scope.row.status != 'completed' "
                  icon="el-icon-edit"
                  v-hasPermi="['contractManagement:technical:change']"
                  @click.native="handleChange(scope.row.id)"
                >修改
              </el-dropdown-item>
              <el-dropdown-item
                  v-show="scope.row.instId"
                  size="mini"
                  type="text"
                  icon="el-icon-s-order"
                  @click.native="newWindowBpm(scope.row.instId,'1')"
                >审批流程
              </el-dropdown-item>
              <el-dropdown-item
                  size="mini"
                  v-show=" scope.row.status != 'active' && scope.row.status != 'completed' "
                  type="text"
                  icon="el-icon-delete"
                  v-hasPermi="['contractManagement:technical:delete']"
                  @click.native="handleDelete(scope.row)"
                >删除
              </el-dropdown-item>
              <el-dropdown-item type="text" size="small" @click.native="sysnc(scope.row)" icon="el-icon-refresh" v-if="scope.row.isLegalSync!=1&&scope.row.status=='completed'" v-hasPermi="['contractManagement:technical:sysnc']">同步
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listForm,getTreeselect,delForm} from "@/api/contractManagement/technical";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import BaseIndex from '@/views/base/BaseIndex'
import {newWindowBpm} from "@/utils/openBpm";
import request from '@/utils/request'
export default {
  name: "Technical",
  components: { Treeselect },
  extends: BaseIndex,
  data() {
    return {
      listHeight:0,
      zoneList:[],
      props:{
        value:'id',
        label:'label'
      }
    };
  },
  watch: {

  },
  created() {
    console.log(this.ids)
    this.getList();
    getTreeselect().then(
      response => {
        if(response && response.code ===0){
          this.zoneList = response.data
        }else{
          this.msgError(response.msg)
        }
        this.loading = false;
      }
    );
  },
  methods: {
    newWindowBpm,
    listForm,
    delForm,
    handleChange(id){  // 修改
      this.$router.push({path: "technicalDetails", query: {
        type:'change',
        id
      }});
    },

    handleAddFn(){
      this.$router.push({path: "technicalDetails", query: {
        type:'add'
      }});
    },
    handleDetails(id,status){
      this.$router.push({path: "technicalDetails", query: {
        type:'details',
        id,
        status: status == 'completed' ? 1 : 0
      }});
    },
    // 同步单个的
    sysnc(item){
      let {id} = item
      let {proId} = item
      this.loading = true
      request({
        url: `/legalSystem/syncContract`,
        method: 'GET',
        params:{
          proId:id,id
        }
      }).then(res => {
        console.log(res)
        if (res.code === 0) {
          this.$message.success('同步成功')
          this.handleQuery();
        }else{
          this.$message.error('同步失败')
        }
        this.loading = false
      }).catch(error => {
        console.log(error)
        this.loading = false
      })
    },
    // 同步所有的的
    sysncAll(){
      this.loading = true
      request({
        url: `/legalSystem/syncAllContract`,
        method: 'GET',
        params:{}
      }).then(res => {
        console.log(res)
        if (res.code === 0) {
          this.$message.success(`成功同步 ${res.data} 条`)
          this.handleQuery();
        }else{
          this.$message.error('同步失败')
        }
        this.loading = false
      }).catch(error => {
        console.log(error)
        this.$message.error('同步失败')
        this.loading = false
      })
    },
  }
};
</script>
